#include "todo_info.h"
#include <QString>


todo_info::todo_info()
{


    lus_tcp->aes_tcp_init();

    lus_tcp->to_addr = to_addr;

    connect(lus_tcp, SIGNAL(read_msg(QByteArray)), this, SLOT(do_msg(QByteArray)));

//    connect(timer, SIGNAL(timeout()), this, SLOT(public_timer_fun()));
 //   timer->start(10000);

  //  connect(lus_tcp, SIGNAL(aes_tcp_connected(void)), this, SLOT(connect_flush(void)));

    //读取配置文件
}

void todo_info::todo_del(int index)
{


    QString cmd = "<user_msg type=\"mirror_msg\" body=\"##\"  flag=\"todo_del\"/>";
    cmd.replace("##", QString::number(index));

    lus_tcp->sendMessage(cmd.toLatin1() , ENCRYPT_YES);
    flush_todo();

}

void todo_info::todo_add(QString todo)
{

    QString cmd = "<user_msg type=\"mirror_msg\" body=\"##\"  addr=\"@@\" flag=\"todo_add\"/>";

        cmd.replace("##", todo.toUtf8().toBase64());
        cmd.replace("@@", to_addr);
        //qDebug()<<cmd;
        lus_tcp->sendMessage(cmd.toLatin1() , ENCRYPT_YES);

        flush_todo();


}

void todo_info::do_msg(QByteArray msg)
{

    QDomDocument xml;
    xml.setContent(msg);

    QDomElement rootnode = xml.documentElement();

    qDebug()<<msg;

    if(rootnode.tagName() == "user_msg"){

        // qDebug()<<"user_msg";
        if(rootnode.attributeNode("type").value() == "mirror_msg"){

            if( rootnode.attributeNode("flag").value() == "todo_list_re"){

                QString raw_list = rootnode.attributeNode("body").value().toUtf8();


                if(raw_list.isEmpty())return;

                QByteArray by = QByteArray::fromBase64(raw_list.toLocal8Bit());

                QString list =   by;

                list = list.left(list.size()-1);
                qDebug()<<"thing "<<list;

                QStringList todo_list =  list.split("#");

                emit emit_group_todo();
                for(int i=0; i<todo_list.size(); i++){

                    qDebug()<<"list: "<<todo_list.at(i);

                    emit emit_add_todo(todo_list.at(i));
                }
            }
        }


        return;
    }


    if(rootnode.tagName() == "init_msg"){

        flush_mirror_faaddr();
        flush_todo();

    }


}



void todo_info::flush_todo(void)
{


    QString cmd = "<user_msg type=\"mirror_msg\" body=\"requst_list\"  flag=\"todo_list\"/>";
    lus_tcp->sendMessage(cmd.toLatin1() , ENCRYPT_YES);

    qDebug()<<"flag1";

}

void todo_info::flush_mirror_faaddr()
{

    QByteArray addr_bc = "<user_msg type=\"from_addr\" body=\"@@\" />";
    addr_bc.replace("@@", lus_tcp->fix_id.toLatin1());
    qDebug()<< addr_bc ;

    lus_tcp->sendMessage(addr_bc, ENCRYPT_YES);


}



void todo_info::connect_flush()
{

    flush_todo();


}
